package edu.hubu.talentmanagementsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.CaseFormat;
import edu.hubu.talentmanagementsystem.mapper.PositionSmallTagMapper;
import edu.hubu.talentmanagementsystem.mapper.context.MapperContext;
import edu.hubu.talentmanagementsystem.model.ext.PositionSmallTag;
import edu.hubu.talentmanagementsystem.service.IPositionSmallTagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import edu.hubu.talentmanagementsystem.common.EncodeOperation;
import edu.hubu.talentmanagementsystem.common.EncodeParam;

import java.util.List;
import java.util.Optional;

/**
 * @author moonlan
 * date 2021/3/2 下午8:45
 */
@SuppressWarnings(value = "unused")
@Service
@Transactional(rollbackFor = Exception.class)
public class PositionSmallTagServiceImpl extends ServiceImpl<PositionSmallTagMapper, PositionSmallTag> implements IPositionSmallTagService {

    private final MapperContext context;

    public PositionSmallTagServiceImpl(MapperContext context) {this.context = context;}

    @Override
    public boolean deleteById(Integer id) {
        return context.getPositionSmallTagMapper().deleteById(id) > 0;
    }

    @Override
    @EncodeOperation
    public Optional<PositionSmallTag> insert(@EncodeParam PositionSmallTag record) {
        int insert = context.getPositionSmallTagMapper().insert(record);
        if (insert > 0) {
            return Optional.of(context.getPositionSmallTagMapper().selectById(record.primaryKeyValue()));
        }
        return Optional.empty();
    }

    @Override
    public Optional<PositionSmallTag> selectById(Integer id) {
        QueryWrapper<PositionSmallTag> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<PositionSmallTag> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(PositionSmallTag::getPositionSmallTagId, id);
        return Optional.of(context.getPositionSmallTagMapper().selectOne(lambdaQueryWrapper));
    }

    @Override
    public List<PositionSmallTag> selectAll(String order) {
        QueryWrapper<PositionSmallTag> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<PositionSmallTag> select = getColumnWrapper(wrapper.lambda());
            return context.getPositionSmallTagMapper().selectList(select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<PositionSmallTag> select = getColumnWrapper(wrapper.lambda());
        return context.getPositionSmallTagMapper().selectList(select);
    }

    @Override
    public Page<PositionSmallTag> selectAllPaged( Integer currentPage,
             Integer pageSize, String order) {
        QueryWrapper<PositionSmallTag> wrapper = new QueryWrapper<>();
        Page<PositionSmallTag> page = new Page<>(currentPage, pageSize);
        if (!StringUtils.hasText(order)) {
            LambdaQueryWrapper<PositionSmallTag> select = getColumnWrapper(wrapper.lambda());
            return context.getPositionSmallTagMapper().selectPage(page, select);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        LambdaQueryWrapper<PositionSmallTag> select = getColumnWrapper(wrapper.lambda());
        return context.getPositionSmallTagMapper().selectPage(page, select);
    }

    private LambdaQueryWrapper<PositionSmallTag> getColumnWrapper(LambdaQueryWrapper<PositionSmallTag> wrapper) {
        return wrapper.select(
                PositionSmallTag::getPositionBigTagId
                    ,
                PositionSmallTag::getPositionBigTag
                    ,
                PositionSmallTag::getPositionSmallTagId
                    ,
                PositionSmallTag::getPositionSmallTagName
        );
    }

    @Override
    public Page<PositionSmallTag> selectLikePaged( Integer currentPage,
             Integer pageSize, PositionSmallTag record, String order) {
        Page<PositionSmallTag> page = new Page<>(currentPage, pageSize);
        QueryWrapper<PositionSmallTag> wrapper = new QueryWrapper<>();
        if (!StringUtils.hasText(order)) {
            return context.getPositionSmallTagMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getPositionSmallTagMapper().selectPage(page, getColumnWrapper(getQueryWrapper(wrapper, record)));
    }

    private LambdaQueryWrapper<PositionSmallTag> getQueryWrapper(QueryWrapper<PositionSmallTag>wrapper, PositionSmallTag record) {
        LambdaQueryWrapper<PositionSmallTag> lambda = wrapper.lambda();
        if (record.getPositionBigTagId() != null) {
            lambda = lambda.eq(PositionSmallTag::getPositionBigTagId,record.getPositionBigTagId());
        }
        if (record.getPositionBigTag() != null) {
            lambda = lambda.eq(PositionSmallTag::getPositionBigTag,record.getPositionBigTag());
        }
        if (record.getPositionSmallTagId() != null) {
            lambda = lambda.eq(PositionSmallTag::getPositionSmallTagId,record.getPositionSmallTagId());
        }
        if (record.getPositionSmallTagName() != null) {
            lambda = lambda.like(PositionSmallTag::getPositionSmallTagName,record.getPositionSmallTagName ());
        }
        return lambda;
    }

    public int count(PositionSmallTag record) {
        return context.getPositionSmallTagMapper().selectCount(getQueryWrapper(new QueryWrapper<>(), record));
    }

    @Override
    @EncodeOperation
    public Optional<PositionSmallTag> updateById(Integer id, @EncodeParam PositionSmallTag record) {
        record.setPositionSmallTagId(id);
        int update = context.getPositionSmallTagMapper().updateById(record);
        if (update > 0) {
            QueryWrapper<PositionSmallTag> wrapper = new QueryWrapper<>();
            LambdaQueryWrapper<PositionSmallTag> lambdaQueryWrapper = getColumnWrapper(wrapper.lambda()).eq(PositionSmallTag::getPositionSmallTagId, id);
            return Optional.of(context.getPositionSmallTagMapper().selectOne(getColumnWrapper(lambdaQueryWrapper)));
        }

        return Optional.empty();
    }

    @Override
    public Page<PositionSmallTag> pagingQueryByBigTagId(Integer currentPage, Integer pageSize, String order,Integer PositionBigTagId) {
        Page<PositionSmallTag> page = new Page<>(currentPage, pageSize);
        QueryWrapper<PositionSmallTag> wrapper = new QueryWrapper<>();
        wrapper.eq("position_big_tag_id",PositionBigTagId);
        if (!StringUtils.hasText(order)) {
            return context.getPositionSmallTagMapper().selectPage(page, wrapper);
        }
        wrapper.orderByAsc(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, order));
        return context.getPositionSmallTagMapper().selectPage(page, wrapper);
    }
}
